package View;

import Model.Action;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;

public class DraggableComponent extends JComponent {

  
    private boolean draggable = true;
  
    protected Point anchorPoint;
  
    protected Cursor draggingCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
 
    protected boolean overbearing = false;
    public int DraggableComponentX=0;
    public int DraggableComponentY=0;
    public MainMenu refmain;
    public int id;
    public DraggableComponent me= this;
    
    public DraggableComponent(int id, MainMenu refmenu) {
        addDragListeners();
        setOpaque(false);
        this.id= id;
        this.refmain = refmenu;
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isOpaque()) {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }
    
    public int getid(){
        return this.id;
    }
    

  
    private void addDragListeners() {
        final DraggableComponent handle = this;
        addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseMoved(MouseEvent e) {
                anchorPoint = e.getPoint();
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                int anchorX = anchorPoint.x;
                int anchorY = anchorPoint.y;

                Point parentOnScreen = getParent().getLocationOnScreen();
                Point mouseOnScreen = e.getLocationOnScreen();
                int x = mouseOnScreen.x - parentOnScreen.x - anchorX;
                int y = mouseOnScreen.y - parentOnScreen.y - anchorY;
                if(y<0){
                    y =0;
                }
                if(y> getParent().getHeight()-250){
                    y = getParent().getHeight()-250;
                }
                if(x<0){
                    x =0;
                }
                if(x> getParent().getWidth()-250){
                    x = getParent().getWidth()-250;
                }
                Point position = new Point(x, y);
                setLocation(position);
                DraggableComponentX=x;
                DraggableComponentY=y;
                //Change Z-Buffer if it is "overbearing"
                if (overbearing) {
                    getParent().setComponentZOrder(handle, 0);
                    repaint();
                }
                refmain.updateDraggableObject(id, me);
            }
        });
    }


    private void removeDragListeners() {
        for (MouseMotionListener listener : this.getMouseMotionListeners()) {
            removeMouseMotionListener(listener);
        }
        setCursor(Cursor.getDefaultCursor());
    }

  
    public boolean isDraggable() {
        return draggable;
    }


    public void setDraggable(boolean draggable) {
        this.draggable = draggable;
        if (draggable) {
            addDragListeners();
        } else {
            removeDragListeners();
        }
    }

 
    public Cursor getDraggingCursor() {
        return draggingCursor;
    }


    public void setDraggingCursor(Cursor draggingCursor) {
        this.draggingCursor = draggingCursor;
    }

    public boolean isOverbearing() {
        return overbearing;
    }


    public void setOverbearing(boolean overbearing) {
        this.overbearing = overbearing;
    }
}
